#include "UPLib\\UP_System.h"
#include "STM32Lib\\stm32f10x.h"
#include "UP_LCD.h"		  
#include "UP_ADC.h"	 	
#include "UP_Exti.h"
#include "UP_Globle.h"
#include "task.h"
#include "pid.h"
#include "senser_state.h"

/*
		灰度:ADC0
	红外测距:ADC1,2,3,4,5,8,9,10,11,12,13,14
	光电开关:ADC15
*/

//PID参数设定
PID_t pRobot={.Kp=-666,.Ki=0,.Kd=0,.Target=0,.Actual=0,.Error0=0,.OutMax=2000,.OutMin=-2000,.IntMax=11,.IntMin=-11,.LeftSpeed=MINSPEED,.RightSpeed=MAXSPEED};
	
int main(void)
{
	UP_System_Init();	//初始化
	//清除标志位
	EXTI->PR=EXTI_Line4;						
	EXTI->PR=EXTI_Line5;	
	//关闭中断
	UP_Exti_ControlIT(EXTI4_IRQn,DISABLE);		
	UP_Exti_ControlIT(EXTI9_5_IRQn,DISABLE);				
	while(1)
	{
		//左上台
		if(UP_ADC_GetValue(13)>UP_VALUE)		
		{	
			OnStage_Left();
			break;
		}
		//右上台
		else if(UP_ADC_GetValue(5)>UP_VALUE)	
		{
			OnStage_Right();
			break;
		}
//		UP_LCD_ShowInt(0,0,UP_ADC_GetValue(1));
//		UP_LCD_ShowInt(7,0,UP_ADC_GetValue(2));
//		UP_LCD_ShowInt(0,1,UP_ADC_GetValue(3));
//		UP_LCD_ShowInt(7,1,UP_ADC_GetValue(4));
//		UP_LCD_ShowInt(0,2,UP_ADC_GetValue(5));
//		UP_LCD_ShowInt(7,2,UP_ADC_GetValue(8));
//		UP_LCD_ShowInt(0,3,UP_ADC_GetValue(9));	
//		UP_LCD_ShowInt(7,3,UP_ADC_GetValue(10));
//		UP_LCD_ClearScreen();
//		
//		UP_LCD_ShowInt(0,0,UP_ADC_GetValue(11));
//		UP_LCD_ShowInt(7,0,UP_ADC_GetValue(12));	
//		UP_LCD_ShowInt(0,1,UP_ADC_GetValue(13));	
//		UP_LCD_ShowInt(7,1,UP_ADC_GetValue(14));	
//		UP_LCD_ShowInt(0,2,GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0));		
//		UP_LCD_ShowInt(7,2,GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4));
//		UP_LCD_ShowInt(0,3,GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5));
		
//		UP_LCD_ShowInt(7,3,Get_GrayValue());	//显示灰度值
//		UP_LCD_ClearScreen();					//清屏
	}	
	//清除标志位
	EXTI->PR=EXTI_Line4;						
	EXTI->PR=EXTI_Line5;						
	//使能中断
	UP_Exti_ControlIT(EXTI4_IRQn,ENABLE);		
	UP_Exti_ControlIT(EXTI9_5_IRQn,ENABLE);		
	
	while(1)
	{
		if(RestartFlag==RESETFLAG)
		{
			EmyCheck(&pRobot);
			PatrArea(&pRobot);
			IsDownTable(&pRobot);
		}
		else
		{
			ComBacToPower();
		}
		UP_LCD_ShowInt(0,0,1);
	}
}
